// pages/user/login/index.js
const app2 = getApp().globalData 
const app = getApp();
import Toast from '../../../miniprogram_npm/tdesign-miniprogram/toast/index';
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const {
  apiBaseUrl
} = require('../../../utils/config.js')

Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    userName:'',
    password:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(wx.getStorageSync('userInfo')){
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },
  nameInput(e){
    this.setData({
      userName:e.detail.value
    })
  },
  namepassword(e){
    this.setData({
      password:e.detail.value
    })
  },
  // 登录
  login(){
    if(this.data.userName == ''){
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请输入用户名',
        icon: 'close-circle',
      });
      return
    }else if(this.data.password == ''){
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请输入密码',
        icon: 'close-circle',
      });
      return
    }
    // 登录
    wx.login({
      success: (res) => {
        console.log(this.data.userName);
        wx.request({
          url: `${apiBaseUrl}Login/user/${this.data.userName}/${this.data.password}`,
          success: res => {
            console.log(res);
            if(res.data.code == 200){
              wx.showToast({
                title: '登录成功！',
                icon:'none'
              })
              wx.setStorageSync('userInfo', res.data.data)
              wx.setStorageSync('isDisplay', true)
              wx.switchTab({
                url: '/pages/index/home/home',
              })
            
            }
            else{
              wx.showToast({
                title: res.data.message,
                icon:'none'
              })
            }
          }
        });
      }
    })
  },
  // 注册
  register(){
    wx.navigateTo({
      url: '/pages/my/newRegester/newRegester',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})